<?php
namespace app\frontend\forms;

use Phalcon\Forms\Form;
use app\models\RecordUpkeep;
use app\frontend\forms\elements\JeDateElement;
use Phalcon\Db\Column;

class RcdUpkeepSearchForm extends Form
{
    public $upkeep_at_from;
    public $upkeep_at_to;

    public function initialize()
    {
        $this->setEntity($this);

        $this->add(
            new JeDateElement(
                "upkeep_at_from"
            )
        );

        $this->add(
            new JeDateElement(
                "upkeep_at_to"
            )
        );
    }

    /**
     * @param \Phalcon\Mvc\Model\Criteria|null $query
     * @return \Phalcon\Mvc\Model\Criteria
     */
    public function getQuery(\Phalcon\Mvc\Model\Criteria $query = null)
    {
        $query = $query?$query:RecordUpkeep::query();
        if($this->upkeep_at_from)
            $query->andWhere("upkeep_at > :upkeep_at_from:",['upkeep_at_from'=>strtotime($this->upkeep_at_from)],['upkeep_at_from'=>Column::BIND_PARAM_INT]);
        if($this->upkeep_at_to)
            $query->andWhere("upkeep_at <:upkeep_at_to:",['upkeep_at_to'=>strtotime($this->upkeep_at_to)],['upkeep_at_to'=>Column::BIND_PARAM_INT]);
    }
}